# flask web 网络编程文件
from flask import Flask, render_template, request
from vsearch import search4letters
from geo import geocode

# html 调用方法：render_template  按照 jinja2 规则
#
def geocode(key,address,city=None,batch=None)->str:
    """获取高德API的地理编码
    注释：key是指高德API的秘钥，请先去注册
          address是指结构化地址，具体请查看：https://lbs.amap.com/api/webservice/guide/api/georegeo
    """
app = Flask(__name__)
import requests,json
def log_request(req: 'flask_request',res: str) -> None:
    with open('zhang_hai_qingriji.py','a')as log:
        # print(req.form,file=log,end="|")
        # print(req.remote_addr,file=log,end="|")
        # print(req.user_agent,file=log,end="|")
        print(req.form, req.remote_addr,req.user_agent,res,file=log,sep=" | ")
# print(str(dir(req)),res,file=log)
# import pandas as pd

zhang_key = "d1e040531c60aaee6a68716f41f088db"
中大南方_address = "广东省广州市中山大学南方学院"
url = "https://restapi.amap.com/v3/geocode/geo?parameters"
params = {
    "key": zhang_key,
    "address":中大南方_address
}
# positional argument follows keyword argument
# pa 一定要放在前面，keyword argument放在末尾！！！可以
response = requests.get(url,params=params)
response
# positional argument follows keyword argument
# pa 一定要放在前面，keyword argument放在末尾！！！可以
response = requests.get(url,params=params)
response
# positional argument follows keyword argument
# pa 一定要放在前面，keyword argument放在末尾！！！可以
response = requests.get(url,params=params)
response
中大南方_location = response.json()['geocodes'][0]['location']
中大南方_location



@app.route('/search4', methods=['POST'])
# 这些数据来自哪里？
def do_search() -> 'html':
    phrase = request.form['phrase']
    letters = request.form['letters']
    address = request.form["address"]
    color1 = request.form['color1']
    color01 = request.form['color01']
    地理坐标 = geocode(zhang_key, address)
    title = 'Here are your results:'
    results = str(search4letters(phrase, letters))
    return render_template('results.html',
                           the_title=title,
                           the_phrase=phrase,
                           the_letters=letters,
                           the_results=results,
                           the_address=地理坐标)


@app.route('/')
@app.route('/entry')
def entry_page() -> 'html':

    title = '欢迎来测试zhichao的元音字母是否存在小功能'
    return render_template('entry.html',
                           the_title=title)


if __name__ == '__main__':
    app.run(debug=True)
